package gat.common.util;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.EnumUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.List;

public class EnumUtil {

    private static final Logger logger = LoggerFactory.getLogger(EnumUtil.class);

    /**
     * 判断某个枚举是否包某个code值
     * @param enumClass 需要判断是否存在那个枚举类中
     * @param code 需要判断的值
     * @return 包含返回true，否则返回false
     */
    public static boolean isInclude(Class enumClass, String methodName, Object code){
        List enumList = EnumUtils.getEnumList(enumClass);
        for (int i = 0;i<enumList.size(); i++){
            Object en = enumList.get(i);
            Class<?> enClass = en.getClass();
            try {
                Method method = enClass.getMethod(methodName);
                Object invoke = method.invoke(en, null);
                if(ObjectUtil.equal(code, invoke)) {
                    return true;
                }
            }catch (Exception e){
                logger.error("枚举执行getCode方法失败...");
            }
        }
        return false;
    }

}
